
from rest_framework import serializers
from .models import Vehicle, VehicleImage
from user_app.serializers import UserSerializer


class VehicleImageSerializer(serializers.ModelSerializer):
    """车辆图片序列化器"""
    
    image_type_display = serializers.CharField(source='get_image_type_display', read_only=True)
    
    class Meta:
        model = VehicleImage
        fields = ['id', 'image_url', 'description', 'image_type', 'image_type_display', 'created_at']
        read_only_fields = ['id', 'image_type_display', 'created_at']


class VehicleSerializer(serializers.ModelSerializer):
    """车辆序列化器"""
    
    images = VehicleImageSerializer(many=True, read_only=True)
    user = UserSerializer(read_only=True)
    status_display = serializers.CharField(source='get_status_display', read_only=True)
    vehicle_type_display = serializers.CharField(source='get_vehicle_type_display', read_only=True)
    
    class Meta:
        model = Vehicle
        fields = [
            'id', 'user', 'license_plate', 'vehicle_type', 'vehicle_type_display',
            'capacity', 'length', 'width', 'height',
            'current_location', 'longitude', 'latitude', 'location_updated_at',
            'status', 'status_display', 'available_time',
            'vehicle_license', 'vehicle_license_image',
            'images', 'created_at', 'updated_at'
        ]
        read_only_fields = [
            'id', 'user', 'status_display', 'vehicle_type_display',
            'location_updated_at', 'created_at', 'updated_at'
        ]


class VehicleCreateSerializer(serializers.ModelSerializer):
    """车辆创建序列化器"""
    
    images = serializers.ListField(
        child=serializers.DictField(
            child=serializers.CharField(),
            allow_empty=False
        ),
        required=False,
        help_text='图片信息列表，每个元素包含image_url、description和image_type'
    )
    
    class Meta:
        model = Vehicle
        fields = [
            'license_plate', 'vehicle_type',
            'capacity', 'length', 'width', 'height',
            'current_location', 'longitude', 'latitude',
            'status', 'available_time',
            'vehicle_license', 'vehicle_license_image',
            'images'
        ]
    
    def create(self, validated_data):
        images_data = validated_data.pop('images', [])
        vehicle = Vehicle.objects.create(**validated_data)
        
        # 创建车辆图片
        for image_data in images_data:
            VehicleImage.objects.create(
                vehicle=vehicle,
                image_url=image_data.get('image_url'),
                description=image_data.get('description'),
                image_type=image_data.get('image_type', VehicleImage.Type.EXTERIOR)
            )
        
        return vehicle


class VehicleUpdateSerializer(serializers.ModelSerializer):
    """车辆更新序列化器"""
    
    class Meta:
        model = Vehicle
        fields = [
            'license_plate', 'vehicle_type',
            'capacity', 'length', 'width', 'height',
            'current_location', 'longitude', 'latitude',
            'status', 'available_time',
            'vehicle_license', 'vehicle_license_image'
        ]


class VehicleImageCreateSerializer(serializers.ModelSerializer):
    """车辆图片创建序列化器"""
    
    class Meta:
        model = VehicleImage
        fields = ['image_url', 'description', 'image_type']


class VehicleLocationUpdateSerializer(serializers.ModelSerializer):
    """车辆位置更新序列化器"""
    
    class Meta:
        model = Vehicle
        fields = ['current_location', 'longitude', 'latitude']
    
    def update(self, instance, validated_data):
        instance.current_location = validated_data.get('current_location', instance.current_location)
        instance.longitude = validated_data.get('longitude', instance.longitude)
        instance.latitude = validated_data.get('latitude', instance.latitude)
        from django.utils import timezone
        instance.location_updated_at = timezone.now()
        instance.save()
        return instance


class VehicleListSerializer(serializers.ModelSerializer):
    """车辆列表序列化器"""
    
    user_name = serializers.CharField(source='user.phone', read_only=True)
    status_display = serializers.CharField(source='get_status_display', read_only=True)
    vehicle_type_display = serializers.CharField(source='get_vehicle_type_display', read_only=True)
    
    class Meta:
        model = Vehicle
        fields = [
            'id', 'user_name', 'license_plate',
            'vehicle_type', 'vehicle_type_display',
            'capacity', 'current_location',
            'status', 'status_display',
            'available_time', 'created_at'
        ]
        read_only_fields = [
            'id', 'user_name', 'status_display',
            'vehicle_type_display', 'created_at'
        ]